<?php

namespace App\Http;

use App\Http\Middleware\Authenticate;
use App\Http\Middleware\CheckForMaintenanceMode;
use App\Http\Middleware\CheckToken;
use App\Http\Middleware\DeveloperOpenMiddleware;
use App\Http\Middleware\EncryptCookies;
use App\Http\Middleware\RedirectIfAuthenticated;
use App\Http\Middleware\ReplaceNullMiddleware;
use App\Http\Middleware\TrimStrings;
use App\Http\Middleware\TrustProxies;
use Barryvdh\Cors\HandleCors;
use Illuminate\Auth\Middleware\AuthenticateWithBasicAuth;
use Illuminate\Auth\Middleware\Authorize;
use Illuminate\Auth\Middleware\EnsureEmailIsVerified;
use Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
use Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull;
use Illuminate\Foundation\Http\Middleware\ValidatePostSize;
use Illuminate\Http\Middleware\SetCacheHeaders;
use Illuminate\Routing\Middleware\SubstituteBindings;
use Illuminate\Routing\Middleware\ThrottleRequests;
use Illuminate\Routing\Middleware\ValidateSignature;
use Illuminate\Session\Middleware\AuthenticateSession;
use Illuminate\Session\Middleware\StartSession;
use Illuminate\View\Middleware\ShareErrorsFromSession;
use Overtrue\LaravelWeChat\Middleware\OAuthAuthenticate;

class Kernel extends HttpKernel
{
    /**
     * The application's global HTTP middleware stack.
     *
     * These middleware are run during every request to your application.
     * 当请求到达后就立马回执行
     * @var array
     */
    protected $middleware = [
        HandleCors::class,
//        \App\Http\Middleware\EnableCrossRequestMiddleware::class,// 为了加速，暂时去掉该组件
        TrustProxies::class,// 为了加速，暂时去掉该组件
        CheckForMaintenanceMode::class,// 为了加速，暂时去掉该组件
        ValidatePostSize::class,// 为了加速，暂时去掉该组件
        TrimStrings::class,// 为了加速，暂时去掉该组件
        ConvertEmptyStringsToNull::class,// 为了加速，暂时去掉该组件
    ];

    /**
     * The application's route middleware groups.
     * 等路由加载完成后就会执行该中间件
     * @var array
     */
    protected $middlewareGroups = [
        'web' => [
            EncryptCookies::class, // 为了加速，暂时去掉该组件
            AddQueuedCookiesToResponse::class,// 为了加速，暂时去掉该组件
            StartSession::class,
            AuthenticateSession::class,
            ShareErrorsFromSession::class,
            CheckToken::class,// 中间件必须放在这里才能获取到session,，开启就验证用户登录以及项目id
            // \App\Http\Middleware\VerifyCsrfToken::class, //禁用或开启csrf_token检验
            SubstituteBindings::class,// 为了加速，暂时去掉该组件
        ],

        'api' => [
            'throttle:60,1',
            'bindings',
        ],
    ];

    /**
     * The application's route middleware.
     *
     * These middleware may be assigned to groups or used individually.
     *
     * @var array
     */
    protected $routeMiddleware = [
        'auth' => Authenticate::class,// 为了加速，暂时去掉该组件
        'auth.basic' => AuthenticateWithBasicAuth::class,// 为了加速，暂时去掉该组件
        'bindings' => SubstituteBindings::class,// 为了加速，暂时去掉该组件
        'cache.headers' => SetCacheHeaders::class,// 为了加速，暂时去掉该组件
        'can' => Authorize::class,
        'guest' => RedirectIfAuthenticated::class,
        'signed' => ValidateSignature::class,
        'throttle' => ThrottleRequests::class,// 为了加速，暂时去掉该组件
        'verified' => EnsureEmailIsVerified::class,// 为了加速，暂时去掉该组件
        'wechat.oauth' => OAuthAuthenticate::class,//
        'replaceNull' => ReplaceNullMiddleware::class,// 中间件 替换掉空值
        'developerOpenMiddleware' => DeveloperOpenMiddleware::class,// 开发者开放接口中间件
        'role' => \Zizaco\Entrust\Middleware\EntrustRole::class,
        'permission' => \Zizaco\Entrust\Middleware\EntrustPermission::class,
        'ability' => \Zizaco\Entrust\Middleware\EntrustAbility::class,
    ];

    /**
     * The priority-sorted list of middleware. 排序中间件  中间件的优先级
     *
     * This forces non-global middleware to always be in the given order.
     *
     * @var array
     */
    protected $middlewarePriority = [
        StartSession::class,
        ShareErrorsFromSession::class,
        Authenticate::class,
        AuthenticateSession::class,
        SubstituteBindings::class,
        Authorize::class,
    ];
}
